package org.openrbac.rbac.controller;

import org.openrbac.rbac.constant.CommonConstant;
import org.openrbac.rbac.controller.base.BaseController;
import org.openrbac.rbac.entity.Auth;
import org.openrbac.rbac.entity.Role;
import org.openrbac.rbac.entity.User;
import org.openrbac.rbac.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author bapie
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserService, User> {


    @GetMapping("/info/")
    public User get(@RequestHeader("username") String username) {
        return service.getUser(username);
    }

    @GetMapping("/info/roles/")
    public List<Role> findUserRoles(@RequestHeader String username) {
        return service.findRoles(username);
    }

    @GetMapping("/info/auths/")
    public List<Auth> findUserAuths(@RequestHeader String username) {
        return service.findAuths(username);
    }

    @PutMapping("/edit/roles")
    public boolean editRoles(@RequestHeader("username") String username, @RequestBody List<Long> roleIds) {
        return service.editRoles(username, roleIds);
    }

    @PostMapping("/getMe")
    public User getMe(@RequestBody User user,HttpServletRequest request) {
        return service.getUser(user,request.getHeader(CommonConstant.USERNAME));
    }
}
